import { Component, Input, OnInit, OnChanges, SimpleChange } from '@angular/core';
import { ApiService } from "@core/services/http/api.service";
import {ApesMessageService} from "@shared/apes/message";
import {deepClone} from "../../../../utils/utils";

@Component({
  selector: 'total-table',
  templateUrl: './total-table.component.html',
  styleUrls: ['./total-table.component.scss']
})
export class TotalTableComponent implements OnInit, OnChanges {
  @Input()
  set parmas(value: any) {
    this.title = value["title"];
    this.titleList = value["titleList"];
    this.tabWidth = value["tabWidth"];
    this.todayLeft = value["todayLeft"];
    this.todayCenter = value["todayCenter"];
    this.todayEnd = value["todayEnd"];
    this.key = value["key"];
    this._parmas = value;
  }

  get parmas() {
    return this._parmas;
  }

  @Input() companyId;

  _parmas: any;
  title: string;
  tabWidth: string;
  todayLeft: string;
  todayCenter: string;
  todayEnd: string;
  key: number;
  titleList: any[] = [];
  reportData: any[] = [];

  allData: any[] = [];
  tablesIndex: number = 0;
  itmeData: any[] = [];

  constructor(private apiService: ApiService,
              private msg: ApesMessageService) {
    const time = (1000 * 60) * 10;
    setInterval(() => {
      this.allData = [];
      this.getData();
    }, time)

    setInterval(() => {
      this.reportData = [];
      this.pageList(this.itmeData);
    }, 5000)
  }

  ngOnInit() {
  }

  ngOnChanges(changes: {[propName: string]: SimpleChange}) {
    this.companyId = changes['companyId'].currentValue;
    this.getData();
  }

  getData() {
    if (this.parmas["api"]) {
      this.allData = [];
      this.itmeData = [];
      let data = {'interfaceMethod': this.parmas["api"], 'pdata': { 'companyId': this.companyId }};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              this.allData = data;
              this.itmeData = data.array;
              let list = deepClone(this.itmeData);
              let listData = [];
              for(let i = 0, len = list.length; i < len; i ++){
                list[i].HFCS = list[i].HFCS + "次";
                list[i]['ZDRQ_SJ'] = this.timesMDHMS(list[i]['ZDRQ_SJ']);
                if(list[i]['BJJG'] == '未询价' || list[i]['BJJG'] == '超时未询价'){
                  listData.push(list[i]);
                }
              }
              if(this.parmas['title'] == '已报价未录入意向及未报价') {
                this.itmeData = listData;
              }else {
                this.itmeData = list;
              }
              this.itmeData = this.itmeData.sort(this.sortarr("YGSC"));
              this.itmeData = this.itmeData.sort(this.sortarr("TIO"));
              this.itmeData = this.itmeData.sort(this.sortarr("TDOT"));
              this.pageList(this.itmeData);
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  // 分页显示
  pageList(arrData: any[]) {
    if(arrData && arrData.length > 0){
      const count = arrData.length;
      this.reportData = [];
      let tempIndex = 0;
      for (let i = this.tablesIndex; i < count; i++) {
        if (tempIndex < 4) {
          this.reportData.push(arrData[i]);
        } else {
          break;
        }
        tempIndex++;
        this.tablesIndex++;
      }
      //当数据遍历完 重新请求数据
      if (this.tablesIndex >= count) {
        this.tablesIndex = 0;
      }
    }
  }

  // 显示 天 时 分 换算
  toHourMinute(minutes) {
    let a = minutes >= 0 ? '' : '-';
    minutes = minutes >= 0 ? minutes : -minutes;

    let day = Math.floor(minutes / 1440);
    let hour = day >= 0
      ? Math.floor((minutes - (day * 1440) ) / 60)
      :Math.floor(minutes / 60);
    let minute = hour >= 0
      ? Math.floor(minutes - (day * 1440) - (hour * 60) )
      : minutes < 0
        ? minutes
        : Math.floor(minutes / 60 / 24);

    let time = "";
    if ( day ) {
      if( day > 10 ){
        time = a +  day + "天";
      }else {
        time = a +  day + "天" + hour + "时" ;
      }
    }else if( hour ){
      time = a +  hour + "时" + minute + "分";
    }else if( minute ){
      time = a +  minute + "分";
    }
    return time;

  }

  // 排序 倒序
  sortarr(key) {
    return function (a, b) {
      a[key] == undefined ? a[key] = 0 :
        b[key] == undefined ? b[key] = 0 : 0 ;
      return b[key] - a[key];
    };
  }

  timesMDHMS(key:string ) {
    if( key !== undefined ){
      key = key.slice(5, 16);
    }
    return key;
  }
}
